package com.hjy.common.hook;

import com.hjy.common.utils.NacosUtils;
import com.hjy.common.utils.ThreadPoolFactory;

/**
 * 用于注销 nacos 注册信息的钩子, 在服务器端注册
 */
public class ShutdownHook {


    private static final ShutdownHook shutdownHook = new ShutdownHook();

    public static ShutdownHook getShutdownHook() {
        return shutdownHook;
    }

    /**
     * 程序关闭时调用, 将Nacos服务信息注销
     */
    public void addClearAllHook() {
        System.out.println("关闭后将自动注销所有服务");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            NacosUtils.clearRegistry();
            ThreadPoolFactory.shutDownAll();
        }));
    }

}
